﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using UserDefinedForms.Entities;
using UserDefinedForms.DAO;

#endregion

namespace UserDefinedForms.DAO.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="FormControlProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class FormControlProviderBaseCore : EntityProviderBase<UserDefinedForms.Entities.FormControl, UserDefinedForms.Entities.FormControlKey>
	{		
		#region Get from Many To Many Relationship Functions
		#region GetBySupplierIdFromComponentSupplier
		
		/// <summary>
		///		Gets FormControls objects from the datasource by SupplierID in the
		///		ComponentSuppliers table. Table FormControls is related to table Suppliers
		///		through the (M:N) relationship defined in the ComponentSuppliers table.
		/// </summary>
		/// <param name="_supplierId"></param>
		/// <returns>Returns a typed collection of FormControl objects.</returns>
		public TList<FormControl> GetBySupplierIdFromComponentSupplier(System.Int32 _supplierId)
		{
			int count = -1;
			return GetBySupplierIdFromComponentSupplier(null,_supplierId, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets UserDefinedForms.Entities.FormControl objects from the datasource by SupplierID in the
		///		ComponentSuppliers table. Table FormControls is related to table Suppliers
		///		through the (M:N) relationship defined in the ComponentSuppliers table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_supplierId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of FormControl objects.</returns>
		public TList<FormControl> GetBySupplierIdFromComponentSupplier(System.Int32 _supplierId, int start, int pageLength)
		{
			int count = -1;
			return GetBySupplierIdFromComponentSupplier(null, _supplierId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets FormControl objects from the datasource by SupplierID in the
		///		ComponentSuppliers table. Table FormControls is related to table Suppliers
		///		through the (M:N) relationship defined in the ComponentSuppliers table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_supplierId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of FormControls objects.</returns>
		public TList<FormControl> GetBySupplierIdFromComponentSupplier(TransactionManager transactionManager, System.Int32 _supplierId)
		{
			int count = -1;
			return GetBySupplierIdFromComponentSupplier(transactionManager, _supplierId, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets FormControl objects from the datasource by SupplierID in the
		///		ComponentSuppliers table. Table FormControls is related to table Suppliers
		///		through the (M:N) relationship defined in the ComponentSuppliers table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_supplierId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of FormControls objects.</returns>
		public TList<FormControl> GetBySupplierIdFromComponentSupplier(TransactionManager transactionManager, System.Int32 _supplierId,int start, int pageLength)
		{
			int count = -1;
			return GetBySupplierIdFromComponentSupplier(transactionManager, _supplierId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets FormControl objects from the datasource by SupplierID in the
		///		ComponentSuppliers table. Table FormControls is related to table Suppliers
		///		through the (M:N) relationship defined in the ComponentSuppliers table.
		/// </summary>
		/// <param name="_supplierId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of FormControls objects.</returns>
		public TList<FormControl> GetBySupplierIdFromComponentSupplier(System.Int32 _supplierId,int start, int pageLength, out int count)
		{
			
			return GetBySupplierIdFromComponentSupplier(null, _supplierId, start, pageLength, out count);
		}


		/// <summary>
		///		Gets FormControls objects from the datasource by SupplierID in the
		///		ComponentSuppliers table. Table FormControls is related to table Suppliers
		///		through the (M:N) relationship defined in the ComponentSuppliers table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="_supplierId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of FormControl objects.</returns>
		public abstract TList<FormControl> GetBySupplierIdFromComponentSupplier(TransactionManager transactionManager,System.Int32 _supplierId, int start, int pageLength, out int count);
		
		#endregion GetBySupplierIdFromComponentSupplier
		
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, UserDefinedForms.Entities.FormControlKey key)
		{
			return Delete(transactionManager, key.ControlId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_controlId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int32 _controlId)
		{
			return Delete(null, _controlId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_controlId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int32 _controlId);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ControlType key.
		///		FK_ControlType Description: 
		/// </summary>
		/// <param name="_controlTypeId"></param>
		/// <returns>Returns a typed collection of UserDefinedForms.Entities.FormControl objects.</returns>
		public TList<FormControl> GetByControlTypeId(System.Int32 _controlTypeId)
		{
			int count = -1;
			return GetByControlTypeId(_controlTypeId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ControlType key.
		///		FK_ControlType Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_controlTypeId"></param>
		/// <returns>Returns a typed collection of UserDefinedForms.Entities.FormControl objects.</returns>
		/// <remarks></remarks>
		public TList<FormControl> GetByControlTypeId(TransactionManager transactionManager, System.Int32 _controlTypeId)
		{
			int count = -1;
			return GetByControlTypeId(transactionManager, _controlTypeId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_ControlType key.
		///		FK_ControlType Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_controlTypeId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of UserDefinedForms.Entities.FormControl objects.</returns>
		public TList<FormControl> GetByControlTypeId(TransactionManager transactionManager, System.Int32 _controlTypeId, int start, int pageLength)
		{
			int count = -1;
			return GetByControlTypeId(transactionManager, _controlTypeId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ControlType key.
		///		fkControlType Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_controlTypeId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of UserDefinedForms.Entities.FormControl objects.</returns>
		public TList<FormControl> GetByControlTypeId(System.Int32 _controlTypeId, int start, int pageLength)
		{
			int count =  -1;
			return GetByControlTypeId(null, _controlTypeId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ControlType key.
		///		fkControlType Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_controlTypeId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of UserDefinedForms.Entities.FormControl objects.</returns>
		public TList<FormControl> GetByControlTypeId(System.Int32 _controlTypeId, int start, int pageLength,out int count)
		{
			return GetByControlTypeId(null, _controlTypeId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ControlType key.
		///		FK_ControlType Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_controlTypeId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of UserDefinedForms.Entities.FormControl objects.</returns>
		public abstract TList<FormControl> GetByControlTypeId(TransactionManager transactionManager, System.Int32 _controlTypeId, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override UserDefinedForms.Entities.FormControl Get(TransactionManager transactionManager, UserDefinedForms.Entities.FormControlKey key, int start, int pageLength)
		{
			return GetByControlId(transactionManager, key.ControlId, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key ControFormIDX index.
		/// </summary>
		/// <param name="_formId"></param>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormControl&gt;"/> class.</returns>
		public TList<FormControl> GetByFormId(System.Int32 _formId)
		{
			int count = -1;
			return GetByFormId(null,_formId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the ControFormIDX index.
		/// </summary>
		/// <param name="_formId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormControl&gt;"/> class.</returns>
		public TList<FormControl> GetByFormId(System.Int32 _formId, int start, int pageLength)
		{
			int count = -1;
			return GetByFormId(null, _formId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the ControFormIDX index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_formId"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormControl&gt;"/> class.</returns>
		public TList<FormControl> GetByFormId(TransactionManager transactionManager, System.Int32 _formId)
		{
			int count = -1;
			return GetByFormId(transactionManager, _formId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the ControFormIDX index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_formId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormControl&gt;"/> class.</returns>
		public TList<FormControl> GetByFormId(TransactionManager transactionManager, System.Int32 _formId, int start, int pageLength)
		{
			int count = -1;
			return GetByFormId(transactionManager, _formId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the ControFormIDX index.
		/// </summary>
		/// <param name="_formId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormControl&gt;"/> class.</returns>
		public TList<FormControl> GetByFormId(System.Int32 _formId, int start, int pageLength, out int count)
		{
			return GetByFormId(null, _formId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the ControFormIDX index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_formId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormControl&gt;"/> class.</returns>
		public abstract TList<FormControl> GetByFormId(TransactionManager transactionManager, System.Int32 _formId, int start, int pageLength, out int count);
						
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key ControlComponentIDX index.
		/// </summary>
		/// <param name="_componentId"></param>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormControl&gt;"/> class.</returns>
		public TList<FormControl> GetByComponentId(System.Int32? _componentId)
		{
			int count = -1;
			return GetByComponentId(null,_componentId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the ControlComponentIDX index.
		/// </summary>
		/// <param name="_componentId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormControl&gt;"/> class.</returns>
		public TList<FormControl> GetByComponentId(System.Int32? _componentId, int start, int pageLength)
		{
			int count = -1;
			return GetByComponentId(null, _componentId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the ControlComponentIDX index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_componentId"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormControl&gt;"/> class.</returns>
		public TList<FormControl> GetByComponentId(TransactionManager transactionManager, System.Int32? _componentId)
		{
			int count = -1;
			return GetByComponentId(transactionManager, _componentId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the ControlComponentIDX index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_componentId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormControl&gt;"/> class.</returns>
		public TList<FormControl> GetByComponentId(TransactionManager transactionManager, System.Int32? _componentId, int start, int pageLength)
		{
			int count = -1;
			return GetByComponentId(transactionManager, _componentId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the ControlComponentIDX index.
		/// </summary>
		/// <param name="_componentId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormControl&gt;"/> class.</returns>
		public TList<FormControl> GetByComponentId(System.Int32? _componentId, int start, int pageLength, out int count)
		{
			return GetByComponentId(null, _componentId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the ControlComponentIDX index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_componentId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="TList&lt;FormControl&gt;"/> class.</returns>
		public abstract TList<FormControl> GetByComponentId(TransactionManager transactionManager, System.Int32? _componentId, int start, int pageLength, out int count);
						
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_FormControl index.
		/// </summary>
		/// <param name="_controlId"></param>
		/// <returns>Returns an instance of the <see cref="UserDefinedForms.Entities.FormControl"/> class.</returns>
		public UserDefinedForms.Entities.FormControl GetByControlId(System.Int32 _controlId)
		{
			int count = -1;
			return GetByControlId(null,_controlId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_FormControl index.
		/// </summary>
		/// <param name="_controlId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="UserDefinedForms.Entities.FormControl"/> class.</returns>
		public UserDefinedForms.Entities.FormControl GetByControlId(System.Int32 _controlId, int start, int pageLength)
		{
			int count = -1;
			return GetByControlId(null, _controlId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_FormControl index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_controlId"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="UserDefinedForms.Entities.FormControl"/> class.</returns>
		public UserDefinedForms.Entities.FormControl GetByControlId(TransactionManager transactionManager, System.Int32 _controlId)
		{
			int count = -1;
			return GetByControlId(transactionManager, _controlId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_FormControl index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_controlId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="UserDefinedForms.Entities.FormControl"/> class.</returns>
		public UserDefinedForms.Entities.FormControl GetByControlId(TransactionManager transactionManager, System.Int32 _controlId, int start, int pageLength)
		{
			int count = -1;
			return GetByControlId(transactionManager, _controlId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_FormControl index.
		/// </summary>
		/// <param name="_controlId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="UserDefinedForms.Entities.FormControl"/> class.</returns>
		public UserDefinedForms.Entities.FormControl GetByControlId(System.Int32 _controlId, int start, int pageLength, out int count)
		{
			return GetByControlId(null, _controlId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_FormControl index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_controlId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="UserDefinedForms.Entities.FormControl"/> class.</returns>
		public abstract UserDefinedForms.Entities.FormControl GetByControlId(TransactionManager transactionManager, System.Int32 _controlId, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;FormControl&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;FormControl&gt;"/></returns>
		public static TList<FormControl> Fill(IDataReader reader, TList<FormControl> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				UserDefinedForms.Entities.FormControl c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("FormControl")
					.Append("|").Append((System.Int32)reader[((int)FormControlColumn.ControlId - 1)]).ToString();
					c = EntityManager.LocateOrCreate<FormControl>(
					key.ToString(), // EntityTrackingKey
					"FormControl",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new UserDefinedForms.Entities.FormControl();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.ControlId = (System.Int32)reader[((int)FormControlColumn.ControlId - 1)];
					c.Name = (reader.IsDBNull(((int)FormControlColumn.Name - 1)))?null:(System.String)reader[((int)FormControlColumn.Name - 1)];
					c.ControlTypeId = (System.Int32)reader[((int)FormControlColumn.ControlTypeId - 1)];
					c.ToolTipText = (reader.IsDBNull(((int)FormControlColumn.ToolTipText - 1)))?null:(System.String)reader[((int)FormControlColumn.ToolTipText - 1)];
					c.Border = (reader.IsDBNull(((int)FormControlColumn.Border - 1)))?null:(System.Boolean?)reader[((int)FormControlColumn.Border - 1)];
					c.Top = (reader.IsDBNull(((int)FormControlColumn.Top - 1)))?null:(System.Int32?)reader[((int)FormControlColumn.Top - 1)];
					c.Left = (reader.IsDBNull(((int)FormControlColumn.Left - 1)))?null:(System.Int32?)reader[((int)FormControlColumn.Left - 1)];
					c.Width = (reader.IsDBNull(((int)FormControlColumn.Width - 1)))?null:(System.Int32?)reader[((int)FormControlColumn.Width - 1)];
					c.Height = (reader.IsDBNull(((int)FormControlColumn.Height - 1)))?null:(System.Int32?)reader[((int)FormControlColumn.Height - 1)];
					c.Font = (reader.IsDBNull(((int)FormControlColumn.Font - 1)))?null:(System.String)reader[((int)FormControlColumn.Font - 1)];
					c.DefaultValue = (reader.IsDBNull(((int)FormControlColumn.DefaultValue - 1)))?null:(System.String)reader[((int)FormControlColumn.DefaultValue - 1)];
					c.Visible = (reader.IsDBNull(((int)FormControlColumn.Visible - 1)))?null:(System.Boolean?)reader[((int)FormControlColumn.Visible - 1)];
					c.Enable = (reader.IsDBNull(((int)FormControlColumn.Enable - 1)))?null:(System.Boolean?)reader[((int)FormControlColumn.Enable - 1)];
					c.IsSummaryField = (reader.IsDBNull(((int)FormControlColumn.IsSummaryField - 1)))?null:(System.Boolean?)reader[((int)FormControlColumn.IsSummaryField - 1)];
					c.IsCommon = (reader.IsDBNull(((int)FormControlColumn.IsCommon - 1)))?null:(System.Boolean?)reader[((int)FormControlColumn.IsCommon - 1)];
					c.IsCode = (reader.IsDBNull(((int)FormControlColumn.IsCode - 1)))?null:(System.Boolean?)reader[((int)FormControlColumn.IsCode - 1)];
					c.IsLayout = (reader.IsDBNull(((int)FormControlColumn.IsLayout - 1)))?null:(System.Boolean?)reader[((int)FormControlColumn.IsLayout - 1)];
					c.ComponentId = (reader.IsDBNull(((int)FormControlColumn.ComponentId - 1)))?null:(System.Int32?)reader[((int)FormControlColumn.ComponentId - 1)];
					c.FormId = (System.Int32)reader[((int)FormControlColumn.FormId - 1)];
					c.FormType = (System.Int32)reader[((int)FormControlColumn.FormType - 1)];
					c.Status = (System.Int32)reader[((int)FormControlColumn.Status - 1)];
					c.Description = (reader.IsDBNull(((int)FormControlColumn.Description - 1)))?null:(System.String)reader[((int)FormControlColumn.Description - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="UserDefinedForms.Entities.FormControl"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="UserDefinedForms.Entities.FormControl"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, UserDefinedForms.Entities.FormControl entity)
		{
			if (!reader.Read()) return;
			
			entity.ControlId = (System.Int32)reader[((int)FormControlColumn.ControlId - 1)];
			entity.Name = (reader.IsDBNull(((int)FormControlColumn.Name - 1)))?null:(System.String)reader[((int)FormControlColumn.Name - 1)];
			entity.ControlTypeId = (System.Int32)reader[((int)FormControlColumn.ControlTypeId - 1)];
			entity.ToolTipText = (reader.IsDBNull(((int)FormControlColumn.ToolTipText - 1)))?null:(System.String)reader[((int)FormControlColumn.ToolTipText - 1)];
			entity.Border = (reader.IsDBNull(((int)FormControlColumn.Border - 1)))?null:(System.Boolean?)reader[((int)FormControlColumn.Border - 1)];
			entity.Top = (reader.IsDBNull(((int)FormControlColumn.Top - 1)))?null:(System.Int32?)reader[((int)FormControlColumn.Top - 1)];
			entity.Left = (reader.IsDBNull(((int)FormControlColumn.Left - 1)))?null:(System.Int32?)reader[((int)FormControlColumn.Left - 1)];
			entity.Width = (reader.IsDBNull(((int)FormControlColumn.Width - 1)))?null:(System.Int32?)reader[((int)FormControlColumn.Width - 1)];
			entity.Height = (reader.IsDBNull(((int)FormControlColumn.Height - 1)))?null:(System.Int32?)reader[((int)FormControlColumn.Height - 1)];
			entity.Font = (reader.IsDBNull(((int)FormControlColumn.Font - 1)))?null:(System.String)reader[((int)FormControlColumn.Font - 1)];
			entity.DefaultValue = (reader.IsDBNull(((int)FormControlColumn.DefaultValue - 1)))?null:(System.String)reader[((int)FormControlColumn.DefaultValue - 1)];
			entity.Visible = (reader.IsDBNull(((int)FormControlColumn.Visible - 1)))?null:(System.Boolean?)reader[((int)FormControlColumn.Visible - 1)];
			entity.Enable = (reader.IsDBNull(((int)FormControlColumn.Enable - 1)))?null:(System.Boolean?)reader[((int)FormControlColumn.Enable - 1)];
			entity.IsSummaryField = (reader.IsDBNull(((int)FormControlColumn.IsSummaryField - 1)))?null:(System.Boolean?)reader[((int)FormControlColumn.IsSummaryField - 1)];
			entity.IsCommon = (reader.IsDBNull(((int)FormControlColumn.IsCommon - 1)))?null:(System.Boolean?)reader[((int)FormControlColumn.IsCommon - 1)];
			entity.IsCode = (reader.IsDBNull(((int)FormControlColumn.IsCode - 1)))?null:(System.Boolean?)reader[((int)FormControlColumn.IsCode - 1)];
			entity.IsLayout = (reader.IsDBNull(((int)FormControlColumn.IsLayout - 1)))?null:(System.Boolean?)reader[((int)FormControlColumn.IsLayout - 1)];
			entity.ComponentId = (reader.IsDBNull(((int)FormControlColumn.ComponentId - 1)))?null:(System.Int32?)reader[((int)FormControlColumn.ComponentId - 1)];
			entity.FormId = (System.Int32)reader[((int)FormControlColumn.FormId - 1)];
			entity.FormType = (System.Int32)reader[((int)FormControlColumn.FormType - 1)];
			entity.Status = (System.Int32)reader[((int)FormControlColumn.Status - 1)];
			entity.Description = (reader.IsDBNull(((int)FormControlColumn.Description - 1)))?null:(System.String)reader[((int)FormControlColumn.Description - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="UserDefinedForms.Entities.FormControl"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="UserDefinedForms.Entities.FormControl"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, UserDefinedForms.Entities.FormControl entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.ControlId = (System.Int32)dataRow["ControlID"];
			entity.Name = Convert.IsDBNull(dataRow["Name"]) ? null : (System.String)dataRow["Name"];
			entity.ControlTypeId = (System.Int32)dataRow["ControlTypeID"];
			entity.ToolTipText = Convert.IsDBNull(dataRow["ToolTipText"]) ? null : (System.String)dataRow["ToolTipText"];
			entity.Border = Convert.IsDBNull(dataRow["Border"]) ? null : (System.Boolean?)dataRow["Border"];
			entity.Top = Convert.IsDBNull(dataRow["Top"]) ? null : (System.Int32?)dataRow["Top"];
			entity.Left = Convert.IsDBNull(dataRow["Left"]) ? null : (System.Int32?)dataRow["Left"];
			entity.Width = Convert.IsDBNull(dataRow["Width"]) ? null : (System.Int32?)dataRow["Width"];
			entity.Height = Convert.IsDBNull(dataRow["Height"]) ? null : (System.Int32?)dataRow["Height"];
			entity.Font = Convert.IsDBNull(dataRow["Font"]) ? null : (System.String)dataRow["Font"];
			entity.DefaultValue = Convert.IsDBNull(dataRow["DefaultValue"]) ? null : (System.String)dataRow["DefaultValue"];
			entity.Visible = Convert.IsDBNull(dataRow["Visible"]) ? null : (System.Boolean?)dataRow["Visible"];
			entity.Enable = Convert.IsDBNull(dataRow["Enable"]) ? null : (System.Boolean?)dataRow["Enable"];
			entity.IsSummaryField = Convert.IsDBNull(dataRow["IsSummaryField"]) ? null : (System.Boolean?)dataRow["IsSummaryField"];
			entity.IsCommon = Convert.IsDBNull(dataRow["IsCommon"]) ? null : (System.Boolean?)dataRow["IsCommon"];
			entity.IsCode = Convert.IsDBNull(dataRow["IsCode"]) ? null : (System.Boolean?)dataRow["IsCode"];
			entity.IsLayout = Convert.IsDBNull(dataRow["IsLayout"]) ? null : (System.Boolean?)dataRow["IsLayout"];
			entity.ComponentId = Convert.IsDBNull(dataRow["ComponentID"]) ? null : (System.Int32?)dataRow["ComponentID"];
			entity.FormId = (System.Int32)dataRow["FormID"];
			entity.FormType = (System.Int32)dataRow["FormType"];
			entity.Status = (System.Int32)dataRow["Status"];
			entity.Description = Convert.IsDBNull(dataRow["Description"]) ? null : (System.String)dataRow["Description"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="UserDefinedForms.Entities.FormControl"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">UserDefinedForms.Entities.FormControl Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, UserDefinedForms.Entities.FormControl entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region ComponentIdSource	
			if (CanDeepLoad(entity, "FormControl|ComponentIdSource", deepLoadType, innerList) 
				&& entity.ComponentIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = (entity.ComponentId ?? (int)0);
				FormControl tmpEntity = EntityManager.LocateEntity<FormControl>(EntityLocator.ConstructKeyFromPkItems(typeof(FormControl), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.ComponentIdSource = tmpEntity;
				else
					entity.ComponentIdSource = DataRepository.FormControlProvider.GetByControlId(transactionManager, (entity.ComponentId ?? (int)0));		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ComponentIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.ComponentIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.FormControlProvider.DeepLoad(transactionManager, entity.ComponentIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion ComponentIdSource

			#region FormIdSource	
			if (CanDeepLoad(entity, "DesignForm|FormIdSource", deepLoadType, innerList) 
				&& entity.FormIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.FormId;
				DesignForm tmpEntity = EntityManager.LocateEntity<DesignForm>(EntityLocator.ConstructKeyFromPkItems(typeof(DesignForm), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.FormIdSource = tmpEntity;
				else
					entity.FormIdSource = DataRepository.DesignFormProvider.GetByFormId(transactionManager, entity.FormId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'FormIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.FormIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.DesignFormProvider.DeepLoad(transactionManager, entity.FormIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion FormIdSource

			#region ControlTypeIdSource	
			if (CanDeepLoad(entity, "ControlType|ControlTypeIdSource", deepLoadType, innerList) 
				&& entity.ControlTypeIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.ControlTypeId;
				ControlType tmpEntity = EntityManager.LocateEntity<ControlType>(EntityLocator.ConstructKeyFromPkItems(typeof(ControlType), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.ControlTypeIdSource = tmpEntity;
				else
					entity.ControlTypeIdSource = DataRepository.ControlTypeProvider.GetByControlTypeId(transactionManager, entity.ControlTypeId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ControlTypeIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.ControlTypeIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.ControlTypeProvider.DeepLoad(transactionManager, entity.ControlTypeIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion ControlTypeIdSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByControlId methods when available
			
			#region SupplierIdSupplierCollection_From_ComponentSupplier
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<Supplier>|SupplierIdSupplierCollection_From_ComponentSupplier", deepLoadType, innerList))
			{
				entity.SupplierIdSupplierCollection_From_ComponentSupplier = DataRepository.SupplierProvider.GetByControlIdFromComponentSupplier(transactionManager, entity.ControlId);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'SupplierIdSupplierCollection_From_ComponentSupplier' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.SupplierIdSupplierCollection_From_ComponentSupplier != null)
				{
					deepHandles.Add("SupplierIdSupplierCollection_From_ComponentSupplier",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< Supplier >) DataRepository.SupplierProvider.DeepLoad,
						new object[] { transactionManager, entity.SupplierIdSupplierCollection_From_ComponentSupplier, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			#region ComponentSupplierCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<ComponentSupplier>|ComponentSupplierCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ComponentSupplierCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.ComponentSupplierCollection = DataRepository.ComponentSupplierProvider.GetByControlId(transactionManager, entity.ControlId);

				if (deep && entity.ComponentSupplierCollection.Count > 0)
				{
					deepHandles.Add("ComponentSupplierCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<ComponentSupplier>) DataRepository.ComponentSupplierProvider.DeepLoad,
						new object[] { transactionManager, entity.ComponentSupplierCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region FormControlCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<FormControl>|FormControlCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'FormControlCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.FormControlCollection = DataRepository.FormControlProvider.GetByComponentId(transactionManager, entity.ControlId);

				if (deep && entity.FormControlCollection.Count > 0)
				{
					deepHandles.Add("FormControlCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<FormControl>) DataRepository.FormControlProvider.DeepLoad,
						new object[] { transactionManager, entity.FormControlCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region FormDataCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<FormData>|FormDataCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'FormDataCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.FormDataCollection = DataRepository.FormDataProvider.GetByControlId(transactionManager, entity.ControlId);

				if (deep && entity.FormDataCollection.Count > 0)
				{
					deepHandles.Add("FormDataCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<FormData>) DataRepository.FormDataProvider.DeepLoad,
						new object[] { transactionManager, entity.FormDataCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region ControlListItemCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<ControlListItem>|ControlListItemCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ControlListItemCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.ControlListItemCollection = DataRepository.ControlListItemProvider.GetByControlId(transactionManager, entity.ControlId);

				if (deep && entity.ControlListItemCollection.Count > 0)
				{
					deepHandles.Add("ControlListItemCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<ControlListItem>) DataRepository.ControlListItemProvider.DeepLoad,
						new object[] { transactionManager, entity.ControlListItemCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the UserDefinedForms.Entities.FormControl object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">UserDefinedForms.Entities.FormControl instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">UserDefinedForms.Entities.FormControl Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, UserDefinedForms.Entities.FormControl entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region ComponentIdSource
			if (CanDeepSave(entity, "FormControl|ComponentIdSource", deepSaveType, innerList) 
				&& entity.ComponentIdSource != null)
			{
				DataRepository.FormControlProvider.Save(transactionManager, entity.ComponentIdSource);
				entity.ComponentId = entity.ComponentIdSource.ControlId;
			}
			#endregion 
			
			#region FormIdSource
			if (CanDeepSave(entity, "DesignForm|FormIdSource", deepSaveType, innerList) 
				&& entity.FormIdSource != null)
			{
				DataRepository.DesignFormProvider.Save(transactionManager, entity.FormIdSource);
				entity.FormId = entity.FormIdSource.FormId;
			}
			#endregion 
			
			#region ControlTypeIdSource
			if (CanDeepSave(entity, "ControlType|ControlTypeIdSource", deepSaveType, innerList) 
				&& entity.ControlTypeIdSource != null)
			{
				DataRepository.ControlTypeProvider.Save(transactionManager, entity.ControlTypeIdSource);
				entity.ControlTypeId = entity.ControlTypeIdSource.ControlTypeId;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();

			#region SupplierIdSupplierCollection_From_ComponentSupplier>
			if (CanDeepSave(entity.SupplierIdSupplierCollection_From_ComponentSupplier, "List<Supplier>|SupplierIdSupplierCollection_From_ComponentSupplier", deepSaveType, innerList))
			{
				if (entity.SupplierIdSupplierCollection_From_ComponentSupplier.Count > 0 || entity.SupplierIdSupplierCollection_From_ComponentSupplier.DeletedItems.Count > 0)
				{
					DataRepository.SupplierProvider.Save(transactionManager, entity.SupplierIdSupplierCollection_From_ComponentSupplier); 
					deepHandles.Add("SupplierIdSupplierCollection_From_ComponentSupplier",
						new KeyValuePair<Delegate, object>((DeepSaveHandle<Supplier>) DataRepository.SupplierProvider.DeepSave,
						new object[] { transactionManager, entity.SupplierIdSupplierCollection_From_ComponentSupplier, deepSaveType, childTypes, innerList }
					));
				}
			}
			#endregion 
	
			#region List<ComponentSupplier>
				if (CanDeepSave(entity.ComponentSupplierCollection, "List<ComponentSupplier>|ComponentSupplierCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(ComponentSupplier child in entity.ComponentSupplierCollection)
					{
						if(child.ControlIdSource != null)
						{
								child.ControlId = child.ControlIdSource.ControlId;
						}

						if(child.SupplierIdSource != null)
						{
								child.SupplierId = child.SupplierIdSource.SupplierId;
						}

					}

					if (entity.ComponentSupplierCollection.Count > 0 || entity.ComponentSupplierCollection.DeletedItems.Count > 0)
					{
						//DataRepository.ComponentSupplierProvider.Save(transactionManager, entity.ComponentSupplierCollection);
						
						deepHandles.Add("ComponentSupplierCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< ComponentSupplier >) DataRepository.ComponentSupplierProvider.DeepSave,
							new object[] { transactionManager, entity.ComponentSupplierCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
	
			#region List<FormControl>
				if (CanDeepSave(entity.FormControlCollection, "List<FormControl>|FormControlCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(FormControl child in entity.FormControlCollection)
					{
						if(child.ComponentIdSource != null)
						{
							child.ComponentId = child.ComponentIdSource.ControlId;
						}
						else
						{
							child.ComponentId = entity.ControlId;
						}

					}

					if (entity.FormControlCollection.Count > 0 || entity.FormControlCollection.DeletedItems.Count > 0)
					{
						//DataRepository.FormControlProvider.Save(transactionManager, entity.FormControlCollection);
						
						deepHandles.Add("FormControlCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< FormControl >) DataRepository.FormControlProvider.DeepSave,
							new object[] { transactionManager, entity.FormControlCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
	
			#region List<FormData>
				if (CanDeepSave(entity.FormDataCollection, "List<FormData>|FormDataCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(FormData child in entity.FormDataCollection)
					{
						if(child.ControlIdSource != null)
						{
							child.ControlId = child.ControlIdSource.ControlId;
						}
						else
						{
							child.ControlId = entity.ControlId;
						}

					}

					if (entity.FormDataCollection.Count > 0 || entity.FormDataCollection.DeletedItems.Count > 0)
					{
						//DataRepository.FormDataProvider.Save(transactionManager, entity.FormDataCollection);
						
						deepHandles.Add("FormDataCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< FormData >) DataRepository.FormDataProvider.DeepSave,
							new object[] { transactionManager, entity.FormDataCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
	
			#region List<ControlListItem>
				if (CanDeepSave(entity.ControlListItemCollection, "List<ControlListItem>|ControlListItemCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(ControlListItem child in entity.ControlListItemCollection)
					{
						if(child.ControlIdSource != null)
						{
							child.ControlId = child.ControlIdSource.ControlId;
						}
						else
						{
							child.ControlId = entity.ControlId;
						}

					}

					if (entity.ControlListItemCollection.Count > 0 || entity.ControlListItemCollection.DeletedItems.Count > 0)
					{
						//DataRepository.ControlListItemProvider.Save(transactionManager, entity.ControlListItemCollection);
						
						deepHandles.Add("ControlListItemCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< ControlListItem >) DataRepository.ControlListItemProvider.DeepSave,
							new object[] { transactionManager, entity.ControlListItemCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region FormControlChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>UserDefinedForms.Entities.FormControl</c>
	///</summary>
	public enum FormControlChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>FormControl</c> at ComponentIdSource
		///</summary>
		[ChildEntityType(typeof(FormControl))]
		FormControl,
			
		///<summary>
		/// Composite Property for <c>DesignForm</c> at FormIdSource
		///</summary>
		[ChildEntityType(typeof(DesignForm))]
		DesignForm,
			
		///<summary>
		/// Composite Property for <c>ControlType</c> at ControlTypeIdSource
		///</summary>
		[ChildEntityType(typeof(ControlType))]
		ControlType,
	
		///<summary>
		/// Collection of <c>FormControl</c> as ManyToMany for SupplierCollection_From_ComponentSupplier
		///</summary>
		[ChildEntityType(typeof(TList<Supplier>))]
		SupplierIdSupplierCollection_From_ComponentSupplier,

		///<summary>
		/// Collection of <c>FormControl</c> as OneToMany for ComponentSupplierCollection
		///</summary>
		[ChildEntityType(typeof(TList<ComponentSupplier>))]
		ComponentSupplierCollection,

		///<summary>
		/// Collection of <c>FormControl</c> as OneToMany for FormControlCollection
		///</summary>
		[ChildEntityType(typeof(TList<FormControl>))]
		FormControlCollection,

		///<summary>
		/// Collection of <c>FormControl</c> as OneToMany for FormDataCollection
		///</summary>
		[ChildEntityType(typeof(TList<FormData>))]
		FormDataCollection,

		///<summary>
		/// Collection of <c>FormControl</c> as OneToMany for ControlListItemCollection
		///</summary>
		[ChildEntityType(typeof(TList<ControlListItem>))]
		ControlListItemCollection,
	}
	
	#endregion FormControlChildEntityTypes
	
	#region FormControlFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;FormControlColumn&gt;"/> class
	/// that is used exclusively with a <see cref="FormControl"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class FormControlFilterBuilder : SqlFilterBuilder<FormControlColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the FormControlFilterBuilder class.
		/// </summary>
		public FormControlFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the FormControlFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public FormControlFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the FormControlFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public FormControlFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion FormControlFilterBuilder
	
	#region FormControlParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;FormControlColumn&gt;"/> class
	/// that is used exclusively with a <see cref="FormControl"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class FormControlParameterBuilder : ParameterizedSqlFilterBuilder<FormControlColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the FormControlParameterBuilder class.
		/// </summary>
		public FormControlParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the FormControlParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public FormControlParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the FormControlParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public FormControlParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion FormControlParameterBuilder
	
	#region FormControlSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;FormControlColumn&gt;"/> class
	/// that is used exclusively with a <see cref="FormControl"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class FormControlSortBuilder : SqlSortBuilder<FormControlColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the FormControlSqlSortBuilder class.
		/// </summary>
		public FormControlSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion FormControlSortBuilder
	
} // end namespace
